package cn.newtouch.basic.controller;

import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.service.IVerifyCodeService;
import cn.newtouch.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码接口
     */
    @RequestMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){
        try {

            String baseStr = verifyCodeService.getVerifyCode(key);

            // 把返回的数据封装到jsonResult中，给页面
            return JsonResult.me().setResultObj(baseStr);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("网络繁忙,获取图型验证码失败！");
        }
    }

    /**
     * 短信验证码接口
     */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.smsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试！");
        }
    }


    /**
     * 返回微信绑定验证码的接口
     */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试！");
        }
    }

}
